package com.molichuxing.services.infrastructure.service.impl;

import com.alibaba.fastjson.JSON;
import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.dto.TabDto;
import com.molichuxing.services.business.dto.request.create.GoodsBaseCreateDto;
import com.molichuxing.services.infrastructure.bean.GoodsBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.GoodsMapper;
import com.molichuxing.services.infrastructure.dto.response.GoodsDto;
import com.molichuxing.services.infrastructure.service.GoodsDealerService;
import com.molichuxing.services.infrastructure.service.GoodsService;
import com.molichuxing.services.property.GoodsForbidSaleEnum;
import com.molichuxing.services.property.GoodsTypeEnum;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author xuelin yi
 * @since 2019-08-12
 */
@Service("goodsService")
public class GoodsServiceImpl implements GoodsService {

    private static final Logger log = Logger.getLogger(GoodsServiceImpl.class);

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private GoodsDealerService goodsDealerService;

    @Override
    public Paged<GoodsDto> getGoodsProductList(GoodsTypeEnum goodsTypeEnum, String code, Integer carStyleId, Integer dealerId, List<Integer> goodsIds, int pageNum, int pageSize) {
        log.info("根据条件查询商品列表。goodsTypeEnum ： ["+ goodsTypeEnum +"]"+ "code:["+code+"]"+"carStyleId:["+ carStyleId +"]");
        Paged<GoodsDto> paged = new Paged<>();
       // paged.setTab(getTabList(dealerId));
        int count = goodsMapper.getGoodsPageCount(goodsTypeEnum.getValue(), carStyleId, code, goodsIds);
        if(count <= 0) {
            return paged;
        }
        List<GoodsBean> pageResult = goodsMapper.getGoodsPage(goodsTypeEnum.getValue(), carStyleId, code, goodsIds,pageNum, pageSize);
        log.info("根据条件查询商品列表 出参：pageResult： "+ JSON.toJSONString(pageResult));
        paged.setList(Convert.toProductBeanDto(pageResult));
        paged.setTotal(count);
        paged.setPageSize(pageSize);
        return paged;
    }


    @Override
    public List<GoodsDto> getDealerGoodsList(GoodsTypeEnum goodsTypeEnum, Integer carStyleId, BigDecimal rental, BigDecimal bond, List<Integer> goodsIds) {
        log.info("根据条件查询商品列表。goodsTypeEnum ： ["+ goodsTypeEnum +"]" +"carStyleId:["+ carStyleId +"]"+"goodsIds:"+goodsIds);
        Integer type = goodsTypeEnum == null?null:goodsTypeEnum.getValue();
        List<GoodsBean> result = goodsMapper.getDealerGoodsList(type, carStyleId, null, rental, bond, goodsIds);
        log.info("根据条件查询商品列表 出参：pageResult： "+ JSON.toJSONString(result));
        return Convert.toProductBeanDto(result);
    }

    @Override
    public int createGoodsProduct(GoodsBaseCreateDto createDto) {
        log.info("新增商品基本信息列表。 入参 createDto ： ["+ createDto +"]");
        GoodsBean goodsBean = Convert.toGoodsProductBean(createDto);
        int result = goodsMapper.create(goodsBean);
        log.info("新增商品基本信息列表。 出参 result ： ["+ result +"]");
        createDto.setId(goodsBean.getId());
        return result;
    }

    @Override
    public GoodsDto getGoodsProduct(int goodsId) {
        return Convert.toProductBeanDto(goodsMapper.getById(goodsId));
    }

    @Override
    public List<GoodsDto> getGoodsProductList(List<Integer> goodsIds, List<Integer> vehicleIds) {
        return Convert.toProductBeanDto(goodsMapper.getByIds(goodsIds, vehicleIds));
    }

    @Override
    public Boolean settingGoodsSale(int goodsId, GoodsForbidSaleEnum goodsForbidSaleEnum) {
        int result = goodsMapper.settingGoodsSale(goodsId, goodsForbidSaleEnum.getValue());
        if(result > 0) {
            return true;
        }
        return false;
    }

    /**
     * 查询有效的商品数量
     * @param goodsType （1：专属，2：订阅）
     * @return
     */
    @Override
    public Integer getNoDeleteGoodsCount(Integer goodsType) {
        return goodsMapper.getNoDeleteGoodsCount(goodsType);
    }


}
